package com.imc.platform.base.role.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.imc.core.utils.MenuUtils;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.menu.pojo.Menu;
import com.imc.platform.base.menu.service.IMenuService;
import com.imc.platform.base.org.pojo.OrgZTreeNode;
import com.imc.platform.base.role.pojo.Role;
import com.imc.platform.base.role.service.IRoleService;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.tuple.ResultDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
*类描述：角色管理
*开发者：QY
*创建时间：2018/8/16 19:02
**/
@Slf4j
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    MenuUtils menuUtils;

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class ReqTuple{
        List<String> memberList;
        String roleId;
        Map<String,Object>parmsMap;
        List<Map<String,Object>> parmsList;
    }

    /**
    *QY 2018/8/17 15:51
    *方法描述：跳转到角色管理页面
    **/
    @GetMapping("/showRolePage")
    public String rolePage(String menuId,Model model,HttpServletRequest request){
        model.addAttribute("menu", menuUtils.getMenuByRoleId(request,menuId));
        return "base/role/role";
    }

    @RequestMapping(value = "/showRoleDetPage",method = RequestMethod.GET)
    public String showRoleDetPage(@RequestParam String roleId, Model model){
        Map<String,Object>parms=new HashMap<>();
        parms.put("roleId",roleId);
        model.addAttribute("parms",parms);
        return "base/role/showRoleDet";
    }


    /**
     *QY 2018/8/20 15:28
     *方法描述：加载跳转角色配置展示页面
     **/
    @RequestMapping(value = "/showOperationPage",method = RequestMethod.GET)
    public String showOperationPage(@RequestParam String roleId, Model model){
        Map<String,Object>parms=new HashMap<>();
        parms.put("roleId",roleId);
        model.addAttribute("parms",parms);
        return "base/role/roleOperation";
    }

    /**
    *QY 2018/8/17 15:51
    *方法描述：查询所有角色信息
    **/
    @GetMapping("/queryRoleList")
    @ResponseBody
    public ResultDTO queryRoleList(Pageable pageable){
        PageInfo<Role>queryRoleList=roleService.queryRoleList(pageable.getPageNumber(),pageable.getPageSize());
        JSONObject map = new JSONObject();
        map.put("total",queryRoleList.getTotal());
        map.put("rows",queryRoleList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /**
    *QY 2018/8/18 10:58
    *方法描述：修改角色状态
    **/
    @RequestMapping(value = "/updateState",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO updateStateByRoleId(@RequestParam String state,@RequestParam String id){
        return ResultDTO.builder().data(roleService.updateState(state,id)).build();
    }

    /**
    *QY 2018/8/22 10:01
    *方法描述：根据角色id获取用户
    **/
    @RequestMapping(value = "/queryUserListByRoleId",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO queryUserListByRoleId(Pageable pageable,@RequestParam String roleId){
        PageInfo<User>queryRoleList=roleService.queryUserListByRoleId(pageable.getPageNumber(),pageable.getPageSize(),roleId);
        JSONObject map = new JSONObject();
        map.put("total",queryRoleList.getTotal());
        map.put("rows",queryRoleList.getList());
        ResultDTO result = ResultDTO.builder().data(map).build();
        return result;
    }

    /**
     *QY 2018/8/20 17:23
     *方法描述：获取所有用户信息
     **/
    @RequestMapping(value = "/queryUserList",method = RequestMethod.GET )
    @ResponseBody
    public ResultDTO queryUserList(Pageable pageable,@RequestParam String roleId){
        PageInfo<User>queryRoleList=roleService.queryUserList(pageable.getPageNumber(),pageable.getPageSize(),roleId);
        JSONObject map = new JSONObject();
        map.put("total",queryRoleList.getTotal());
        map.put("rows",queryRoleList.getList());
        return ResultDTO.builder().data(map).build();
    }

    /**
     *QY 2018/8/20 15:28
     *方法描述：加载跳转角色成员添加页面
     **/
    @RequestMapping(value = "/showAddRoleMemberPage",method = RequestMethod.GET)
    public String showAddRoleMemberPage(@RequestParam String roleId,Model model,HttpServletRequest request){
        model.addAttribute("roleId",roleId);
        return "base/role/addRoleMember";
    }

    /**
    *QY 2018/8/20 17:23
    *方法描述：添加成员
    **/
    @RequestMapping(value = "/addMemberList",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO addMemberList(@RequestBody ReqTuple reqTuple){
        return  ResultDTO.builder()
                .data(roleService.insertRoleMember(reqTuple.getMemberList(),reqTuple.getRoleId()))
                .build();
    }

    /**
    *QY 2018/8/29 14:43
    *方法描述：删除角色
    **/
    @RequestMapping(value = "/delRole",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO delRole (@RequestParam String id){
        return  ResultDTO.builder()
                .data(roleService.delRole(id))
                .build();
    }

    /**
     *QY 2018/8/20 17:23
     *方法描述：删除成员
     **/
    @RequestMapping(value = "/delMemberList",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO delMemberList(@RequestBody ReqTuple reqTuple){
        return  ResultDTO.builder()
                .data(roleService.delRoleMember(reqTuple.getMemberList(),reqTuple.getRoleId()))
                .build();
    }

    /**
    *QY 2018/8/26 15:13
    *方法描述：跳转到添加角色页面
    **/
    @RequestMapping(value = "/showAddRolePage",method = RequestMethod.GET)
    public String showAddRolePage(){
        return "base/role/addRole";
    }

    /**
    *QY 2018/8/27 11:20
    *方法描述：删除角色成员
    **/
    @RequestMapping(value = "/delRoleUserById",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO delRoleUserById(@RequestParam String userId,@RequestParam String roleId){
        return  ResultDTO.builder()
                .data(roleService.delRoleUserById(roleId,userId))
                .build();
    }

    /**
    *QY 2018/8/27 15:10
    *方法描述：根據用戶名查詢用戶想詳情
    **/
    @RequestMapping(value = "/queryUserByUserName",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO queryUserByUserName(Pageable pageable,@RequestParam String sel,@RequestParam String orgName,@RequestParam String duty){
        PageInfo<User> res = roleService.queryUserByUserName(pageable.getPageNumber(),pageable.getPageSize(),sel,orgName,duty);
        JSONObject map = new JSONObject();
        map.put("total",res.getTotal());
        map.put("rows",res.getList());
        return  ResultDTO.builder()
                .data(map)
                .build();
    }

    /**
    *QY 2018/8/28 15:46
    *方法描述：校验用户名称
    **/
    @RequestMapping(value = "/queryRoleNameByText",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryRoleNameByText(@RequestParam String roleName){
        return  ResultDTO.builder()
                .data(roleService.queryRoleNameByText(roleName))
                .build();
    }

    /**
    *QY 2018/9/4 15:16
    *方法描述：添加角色
    **/
    @RequestMapping(value = "addRole",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO addRole(@RequestBody  ReqTuple reqTuple){
        return  ResultDTO.builder()
                .data(roleService.addRole(reqTuple.getParmsMap()))
                .build();
    }

    /**
    *QY 2018/9/4 15:16
    *方法描述：根据用户名称查用户详情
    **/
    @RequestMapping(value = "/queryUserBySelectUserName",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO queryUserBySelectUserName(Pageable pageable,@RequestParam String sel,@RequestParam String roleId){
        PageInfo<User> res = roleService.queryUserBySelectUserName(pageable.getPageNumber(),pageable.getPageSize(),sel,roleId);
        JSONObject map = new JSONObject();
        map.put("total",res.getTotal());
        map.put("rows",res.getList());
        return  ResultDTO.builder()
                .data(map)
                .build();
    }

    /**
    *QY 2018/9/4 15:17
    *方法描述：根据角色查已选菜单
    **/
    @RequestMapping(value = "/queryMenuByRoleId",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryMenuByRoleId(@RequestParam String roleId){
        List<OrgZTreeNode>resNodeList=new ArrayList<>();
        List<Menu>resMenuList=menuService.queryMenuAll();
        List<String>queryRoleMenu=new ArrayList<>();
        if(!roleId.equals("")){
            queryRoleMenu =menuService.queryRoleMenuByRoleId(roleId);
        }
        for(Menu menu :resMenuList){
            OrgZTreeNode zTreeNode=new OrgZTreeNode();
            zTreeNode.setId(menu.getId());
            zTreeNode.setPid(menu.getParentId());
            zTreeNode.setName(menu.getMenuName());
            if(queryRoleMenu.size()>0){
                for(String orgId:queryRoleMenu){
                    if(orgId.equals(menu.getId())){
                        zTreeNode.setChecked(true);
                    }
                }
            }
            resNodeList.add(zTreeNode);
        }
        return  ResultDTO.builder()
                .data(resNodeList)
                .build();
    }

    @RequestMapping(value = "/addRoleMenu",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO addRoleMenu(@RequestBody ReqTuple reqTuple){
        return  ResultDTO.builder()
                .data(roleService.addRoleMenu(reqTuple.getParmsList(),reqTuple.roleId))
                .build();
    }

    @RequestMapping(value = "/queryRoleIdByUserId",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryRoleIdByUserId(@RequestParam String userId){
        return  ResultDTO.builder()
                .data(roleService.queryRoleIdByUserId(userId))
                .build();
    }

    @GetMapping("/queryBtnRoleByRoleId")
    @ResponseBody
    public ResultDTO queryBtnRoleByRoleId(String roleId){
//         return ResultDTO.builder().data().build();
        return null;
    }
}
